package com.hyfd.service.mp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.hyfd.common.BaseJson;
import com.hyfd.common.Page;
import com.hyfd.dao.mp.DataPkgDao;
import com.hyfd.dao.mp.ProviderDao;
import com.hyfd.dao.mp.ProviderDataPkgDao;
import com.hyfd.service.BaseService;

/**
 * @功能描述：	流量通道组相关业务
 *
 * @作者：zhangpj		@创建时间：2017年05月11日
 */
@Service
@Transactional
public class ProviderDataPkgSer extends BaseService{
	
	public Logger log = Logger.getLogger(this.getClass());

	@Autowired
	ProviderDataPkgDao providerDataPkgDao;
	@Autowired
	ProviderDao providerDao;
	@Autowired
	DataPkgDao dataPkgDao;
	/**
	 * @功能描述：	根据运营商、流量使用范围、充值地区 获取对应的流量包信息
	 *
	 * @作者：zhangpj		@创建时间：2017年5月11日
	 * @param req
	 * @return
	 */
	public String selectProviderDataPkg(HttpServletRequest req){
		//封装前台参数为map
		Map<String, Object> m=getMaps(req); 
		
		// 根据运营商、流量使用范围、充值地区 获取对应的流量包信息
		List<Map<String, Object>> providerDataPkgList = providerDataPkgDao.selectProviderDataPkg(m);
		
		return JSONObject.toJSONString(providerDataPkgList);
	}
	
	public String providerDataPkgEditPage(String id, HttpServletRequest req){
		try {
			Map<String, Object> provider = providerDao.getProviderById(id);
			req.setAttribute("provider", provider);
		} catch (Exception e) {
			getMyLog(e,log);
		}
		return "mp/providerDataPkgEdit";
	}
	
	public String providerDataPkgEdit(String id, HttpServletRequest req){
		String str="";
		try {
			Map<String, Object> maps = getMaps(req);
			String provinceCode = (String) maps.get("provinceCode");
			String selectedMeg = (String) maps.get("selectedMeg");//选中的流量包
			String uncheckedMeg = (String) maps.get("uncheckedMeg");//未选中的流量包
			if(id!=null&&provinceCode!=null&&selectedMeg!=null&&uncheckedMeg!=null){
				str = providerDataPkgEdit(id,provinceCode,selectedMeg,uncheckedMeg);
			}
		} catch (Exception e) {
			getMyLog(e,log);
		}
		return str;
	}
	
	/**
	 * 漫游流量包修改
	 * */
	public String providerDataPkgEdit(String providerId,String provinceCode,String selectedMeg,String uncheckedMeg){
		String str = "";
		try {
			String[] selectedPkgId = selectedMeg.split(",");
			String[] uncheckedPkgId = uncheckedMeg.split(",");
			for(String uncheckedId:uncheckedPkgId){//获取前台未选中的包ID
				if(uncheckedId!=""){
					Map<String, Object> param = new HashMap<String, Object>();
					param.put("providerId", providerId);
					param.put("provinceCode", provinceCode);
					param.put("dataPkgId", uncheckedId);
					//查询该运营商在该地区有没有这个包
					List<Map<String, Object>> selectAll = providerDataPkgDao.selectAll(param);
					if(selectAll.size()>0){//如果有，则删除
						int i = providerDataPkgDao.deleteByPidPcodePkgId(param);
						if(i==0){
							break;
						}
					}
				}
			}
			for(String selectedId:selectedPkgId){//获取前台选中的包ID
				if(selectedId!=""){
					Map<String, Object> param = new HashMap<String, Object>();
					param.put("providerId", providerId);
					param.put("provinceCode", provinceCode);
					param.put("dataPkgId", selectedId);
					//查询该运营商在该地区有没有这个包
					List<Map<String, Object>> selectAll = providerDataPkgDao.selectAll(param);
					if(selectAll.size()==0){//如果没有则添加
						int i = providerDataPkgDao.insertSelective(param);
						if(i==0){
							break;
						}
					}
				}
			}
			str="true";
		} catch (Exception e) {
			str="false";
			getMyLog(e,log);
		}
		return str;
	}
	
	/**
	 * 运营商流量包数据
	 * @param id
	 * @return
	 */
	public String providerDataPkg(String id,HttpServletRequest req){
		StringBuilder sb=new StringBuilder();
		try{
		Map<String, Object> m=getMaps(req); //封装前台参数为map
		m.put("providerId", id);
		Page p=getPage(m);//提取分页参数
		int total=getProviderDataPkgCount(m);
		p.setCount(total);
		int pageNum=p.getCurrentPage();
		int pageSize=p.getPageSize();
		
		sb.append("{");
		sb.append(""+getKey("page")+":"+p.getCurrentPage()+",");
		sb.append(""+getKey("total")+":"+p.getNumCount()+",");
		sb.append(""+getKey("records")+":"+p.getCount()+",");
		sb.append(""+getKey("rows")+":"+"");
		
		PageHelper.startPage(pageNum, pageSize);//mybatis分页插件
		List<Map<String, Object>> dataList=providerDataPkgDao.selectAll(m);
		String dataListJson=BaseJson.listToJson(dataList);
		sb.append(dataListJson);
		sb.append("}");
		}catch(Exception e){
			getMyLog(e,log);
		}
		
		return sb.toString();
	}
	
	public String providerDataPkgGet(HttpServletRequest req){
		String str ="";
		try {
			Map<String, Object> maps = getMaps(req);
			List<Map<String, Object>> dataPkg = dataPkgDao.getDataPkgByProId(maps);
			str=BaseJson.listToJson(dataPkg);
		} catch (Exception e) {
			getMyLog(e,log);
		}
		return str;
	}
	
	/**
	 * 运营商流量包列表页面
	 * @param id
	 * @return
	 */
	public String providerDataPkgList(String id,HttpServletRequest req){
		try{
			req.setAttribute("id", id);
		}catch(Exception e){
			getMyLog(e,log);
		}
		return "mp/providerDataPkgList";
	}
	
	public int getProviderDataPkgCount(Map<String, Object> m){
		int providerCount=0;
		try{
			providerCount=providerDataPkgDao.selectCount(m);
		}catch(Exception e){
			getMyLog(e,log);
		}
		return providerCount;
	}
	
}
